import Constants

/**
 * System User entity.
 */
class User {
	String userName
	String password
	String firstName
	String department
	Calendar creationTime = Calendar.getInstance()

	static mapping = {
		table Constants.DOMAIN_TABLE_PREFIX + '_USER'
		roles joinTable: Constants.DOMAIN_TABLE_PREFIX + '_USER_ROLE'
	}

	static hasMany = [roles: Role]

	static constraints = {
		userName(unique:true, blank:false, size:4..16)
		// MD5
		password(blank:false, maxSize:32)
		firstName(blank:false, maxSize:16)
		department(nullable:true, maxSize:32)
	}

	def beforeInsert = {
		password = password.encodeAsMD5()
	}


	String toString() {
		"${this.firstName}"
	}
}
